// 引入mongoose第三方模块
const mongoose = require('mongoose');
//引入string-random
const stringRandom = require("string-random");


// 创建用户集合规则
const CommoditySchema = new mongoose.Schema({
    //商品编号
    Art_No:{
        type: String,
        unique: true,
        require: true
    },
    //用户id
    user_id:{
        type: String,
        require: true
    },
    ISBN:{
        type: String,
        required: true,
    },
    //品相
    appearance:{
        type: String
    },
    //商品价格
    customPrice:{
        type: String
    },
    //运费
    freight:{
        type: String
    },
    //发货地址
    deliveryLocation:{
        type: String
    },
    evaluation:{
        type: Array
    }
});

// 创建集合
const Commodity = mongoose.model('Commodity', CommoditySchema);
const path = require('path')

async function createAtrNo(){
    var Art_No = stringRandom(16, { numbers: true });
    var artNo = await Commodity.findOne({Art_No:Art_No},{Art_No:1});
    while(artNo !== null){
        Art_No = stringRandom(16, { numbers: true });
        artNo = await Commodity.findOne({Art_No:Art_No},{Art_No:1});
    }
    
    return Art_No;
}
createAtrNo();


async function createBook () {
    var Art_No = stringRandom(16, { numbers: true });
    var res = await Commodity.findOne({Art_No:Art_No},{Art_No:1});
    while(res !== null){
        Art_No = stringRandom(16, { numbers: true });
        res = await Commodity.findOne({Art_No:Art_No},{Art_No:1});
    }
	const commodity = await Commodity.create({
        //商品编号唯一
        Art_No: Art_No,
        user_id:"4e996860-9770-11eb-817c-cdd0e9e9f010",
        ISBN: "9787553755854",
        appearance:"九成新",
        customPrice:"26.00",
        freight:"7",
        deliveryLocation:"广东广州",
        evaluation: [{
            "user_id": "4e64f570-9771-11eb-b24b-81f91fa92eb4",
            "comments": "快递非常快",
            "commentImg": []
        },
        {
          "user_id": "62914bc0-9771-11eb-bd98-07ffb7bf58b0",
            "comments": "书的质量很好",
            "commentImg": []
        },
        {
           "user_id": "bd1651e0-9770-11eb-8d47-7d91ba0b901a",
            "comments": "包装不错，是好书",
            "commentImg": []
        }
        ],
	});
    console.log("数据添加成功!");
}

//createBook();


// 将用户集合做为模块成员进行导出
module.exports = {
   Commodity
}; 